using System.Web;
using DMS.ServiceLocation;
using Microsoft.Practices.ServiceLocation;

namespace DMS.Web.DataSources
{
	public class ServiceLocatorHelper
	{
		const string ServiceHelperKey = "ServiceHelperKey";
		private readonly IServiceLocator serviceLocator;

		private ServiceLocatorHelper()
		{
			serviceLocator = ServiceLocatorFactory.GetServiceLocator();
		}

		static ServiceLocatorHelper GetInstance()
		{
			ServiceLocatorHelper instance = HttpContext.Current.Application[ServiceHelperKey] as ServiceLocatorHelper;
			if (instance == null)
			{
				instance = new ServiceLocatorHelper();
				HttpContext.Current.Application.Set(ServiceHelperKey, instance); 
			}
			return instance;
		}

		public static T GetService<T>()
		{
			ServiceLocatorHelper locatorHelper = GetInstance();
			return locatorHelper.serviceLocator.GetInstance<T>();
		}
	}
}